
#include <cstdio>
#include <BOpenGL>
#include "IGLState.h"

using namespace BWE;

IGLState::IGLState()
{
	frameBuffer = 0;
}
IGLState::~IGLState()
{

}

void IGLState::save()
{
	glGetIntegerv(GL_FRAMEBUFFER_BINDING, &frameBuffer);
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	glGetIntegerv(GL_VIEWPORT, viewport);
#ifdef REAL_IS_DOUBLE
	glMatrixMode(GL_TEXTURE);
	glGetDoublev(GL_TEXTURE_MATRIX, (GLdouble*)&textureMatrix);
	glMatrixMode(GL_PROJECTION);
	glGetDoublev(GL_PROJECTION_MATRIX, (GLdouble*)&projectionMatrix);
	glMatrixMode(GL_MODELVIEW);
	glGetDoublev(GL_MODELVIEW_MATRIX, (GLdouble*)&modelViewMatrix);
#else
	glMatrixMode(GL_TEXTURE);
	glGetFloatv(GL_TEXTURE_MATRIX, (GLfloat*)&textureMatrix);
	glMatrixMode(GL_PROJECTION);
	glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&projectionMatrix);
	glMatrixMode(GL_MODELVIEW);
	glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&modelViewMatrix);
#endif
}
void IGLState::open()
{
#ifdef REAL_IS_DOUBLE
	glMatrixMode(GL_TEXTURE);
	glLoadMatrixd((GLdouble*)&textureMatrix);
	glMatrixMode(GL_PROJECTION);
	glLoadMatrixd((GLdouble*)&projectionMatrix);
	glMatrixMode(GL_MODELVIEW);
	glLoadMatrixd((GLdouble*)&modelViewMatrix);
#else
	glMatrixMode(GL_TEXTURE);
	glLoadMatrixf((GLfloat*)&textureMatrix);
	glMatrixMode(GL_PROJECTION);
	glLoadMatrixf((GLfloat*)&projectionMatrix);
	glMatrixMode(GL_MODELVIEW);
	glLoadMatrixf((GLfloat*)&modelViewMatrix);
#endif
	glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
	glPopAttrib();
	glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
}
